﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework;

namespace Jemgine.EffectEditor
{
	public partial class EffectRenderControl : GraphicsDeviceControl
	{
        EditorState ES;
        Particles.ParticleRenderer ParticleSystem = new Particles.ParticleRenderer();
        ContentManager Content;
        bool Initialized = false;
        System.Threading.Timer Timer;
        Jemgine.Engine.Renderer.Camera Camera = new Engine.Renderer.Camera(Vector2.One);

		public EffectRenderControl()
		{
			InitializeComponent();
		}

        public void SetEditorState(EditorState ES)
        {
            this.ES = ES;
            ES.EffectLoaded += InitializeEffect;
            ES.InitializeEffect += InitializeEffect;
        }

        private void InitializeEffect()
        {
            if (Initialized)
            {
                ES.Effect.ReleaseBuffers();
                ES.Effect.Initialize(GraphicsDevice, ES.Content);
            }
        }

		protected override void GDCInitialize()
		{
            Content = new ContentManager(Services);

            ParticleSystem.Initialize(GraphicsDevice, Content);
            ES.Effect.Initialize(GraphicsDevice, ES.Content);
           	base.GDCInitialize();

            Timer = new System.Threading.Timer((A) => Invalidate(), null, TimeSpan.FromSeconds(0),
                TimeSpan.FromSeconds(1.0f / 120.0f));

            Initialized = true;
            
		}

        int LastDraw = System.Environment.TickCount;

        protected override void GDCDraw()
        {
            try
            {
                int ElapsedMilliseconds = System.Environment.TickCount - LastDraw;
                LastDraw = System.Environment.TickCount;

                if (IsMouseDown) ES.Effect.Emit(MousePos, 0);
                ES.Effect.Update( (float)ElapsedMilliseconds / 1000.0f );

                Camera.viewportDimensions = new Vector2(Width, Height);
                Camera.zoom = ES.GetZoomFactor();

                this.GraphicsDevice.Clear(Microsoft.Xna.Framework.Color.Black);

                ParticleSystem.View = Camera.View;
                ParticleSystem.World = Camera.World;
                ParticleSystem.Projection = Camera.Projection;

                ParticleSystem.Draw(ES.Effect);

                ES.UpdateStatus();

                base.GDCDraw();
            }
            catch (Exception e)
            {
                Timer.Change(System.Threading.Timeout.Infinite, System.Threading.Timeout.Infinite);
                MessageBox.Show(e.Message);
                Timer.Change(TimeSpan.FromSeconds(0), TimeSpan.FromSeconds(1.0f / 120.0f));
            }
        }

		private void MapRenderControl_SizeChanged(object sender, EventArgs e)
		{
            Camera.viewportDimensions = new Vector2(Width, Height);
		}

        bool IsMouseDown = false;
        Vector2 MousePos = Vector2.Zero;
       
        
		private void MapRenderControl_MouseDown(object sender, MouseEventArgs e)
		{
            MousePos = Camera.UnProject(new Vector2(e.X, e.Y));

            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                IsMouseDown = true;
            }
		}


		private void MapRenderControl_MouseUp(object sender, MouseEventArgs e)
		{
            MousePos = Camera.UnProject(new Vector2(e.X, e.Y));

            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                IsMouseDown = false;
            }
		}

		private void MapRenderControl_MouseMove(object sender, MouseEventArgs e)
		{
            MousePos = Camera.UnProject(new Vector2(e.X, e.Y));
        }


        private void MapRenderControl_MouseEnter(object sender, EventArgs e)
        {
            //this.Focus();
        }

        private void EffectRenderControl_MouseHover(object sender, EventArgs e)
        {
            
        }
    }
}
